#!/bin/sh
# Begin services/dhclient

# Origianlly based upon lfs-bootscripts-1.12 $NETWORK_DEVICES/if{down,up}
# Rewritten by Nathan Coulson <nathan@linuxfromscratch.org>
# Adapted for dhclient by DJ Lucas <dj@linuxfromscratch.org>
# Update for LFS 7.0 by Ken Moffat <ken@linuxfromscratch.org>

# Call with: IFCONFIG=<filename> /lib/services/dhclient <IFACE> <up | down>

#$LastChangedBy: bdubbs $
#$Date: 2019-05-24 14:17:20 -0500 (Fri, 24 May 2019) $

. /lib/lsb/init-functions
. $IFCONFIG

PIDFILE=/run/dhclient-$1.pid
LFILE=/var/lib/dhclient/dhclient-$1.leases
GETLEASEINFO="no"

is_true()
{
   [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ] ||
   [ "$1" = "y" ] || [ "$1" = "t" ]
}

case "$2" in
   up)

     if [ -e ${PIDFILE} ]; then
        ps $(cat ${PIDFILE}) | grep dhclient >/dev/null
        if [ "$?" = "0" ]; then
           log_warning_msg "dhclient appears to be running on $1"
           exit 0
        else
           rm ${PIDFILE}
        fi
     fi

      log_info_msg "Starting dhclient on the $1 interface..."

      /sbin/dhclient -lf ${LFILE} -pf ${PIDFILE} $DHCP_START $1 > /dev/null 2>&1

      if [ "$?" != "0" ]; then
        log_failure_msg2
        exit 1
      fi

      evaluate_retval

      # Print the assigned settings if requested
      is_true "$PRINTIP" && GETLEASEINFO="yes"
      is_true "$PRINTALL" && GETLEASEINFO="yes"

      if [ "${GETLEASEINFO}" == "yes" ]; then
        # Get info from dhclient.leases file
        LEASEINFO=`sed -e :a -e '$q;N;17,$D;ba' ${LFILE}`

        IPADDR=`echo "${LEASEINFO}" | grep "fixed-address" | \
          sed 's/ fixed-address //' | \
          sed 's/\;//'`

        NETMASK=`echo "${LEASEINFO}" | grep "subnet-mask" | \
          sed 's/ option subnet-mask //' | \
          sed 's/\;//'`

        GATEWAY=`echo "${LEASEINFO}" | grep "routers" | \
          sed 's/ option routers //' | \
          sed 's/\;//'`

        DNS=`echo "${LEASEINFO}" | grep "domain-name-servers" | \
          sed 's/ option domain-name-servers //' | \
          sed 's/\;//' | sed 's/,/ and /'`

        if [ "$PRINTALL" = "yes" ]; then
           log_info_msg "IP Address:      ${INFO}${IPADDR}${NORMAL}\n"
           log_info_msg "Subnet Mask:     ${INFO}${NETMASK}${NORMAL}\n"
           log_info_msg "Default Gateway: ${INFO}${GATEWAY}${NORMAL}\n"
           log_info_msg "DNS Server(s):   ${INFO}${DNS}${NORMAL}\n"
        else
           log_info_msg "IP Addresss:     ${INFO}${IPADDR}${NORMAL}\n"
        fi
      fi
   ;;

   down)
      if [ ! -e ${PIDFILE} ]; then
         log_warning_msg "dhclient doesn't appear to be running on $1"
         exit 0
      fi

      log_info_msg "Stopping dhclient on the $1 interface..."

      /sbin/dhclient -r -lf ${LFILE} -pf ${PIDFILE} $DHCP_STOP $1 > /dev/null 2>&1

      if [ -e ${PIDFILE} ]; then 
         ps $(cat ${PIDFILE}) | grep dhclient >/dev/null
         if [ "$?" != "0" ]; then
            rm -f ${PIDFILE}
         fi
      fi

      evaluate_retval
   ;;

   *)
      echo "Usage: $0 [interface] {up|down}"
      exit 1
   ;;
esac

# End services/dhclient
